package com.pomodorotechnique.Bll;

import java.util.Date;
import java.util.UUID;

import android.content.Context;
import android.database.Cursor;

import com.pomodorotechnique.Data.*;
import com.pomodorotechnique.Entity.Model_Allocation;
import com.pomodorotechnique.Entity.Model_Task;
import com.pomodorotechnique.toolkit.Tool;

public class Task {

	private Context context;
	
	public IDataBaseGS<Model_Task,UUID> task_gs;
	public IDataBaseGS<Model_Allocation,UUID> allocation_gs;
	
	public int affectedRows_task = 0;
	public int affectedRows_allocation = 0;
	
	public interface onFinishInsertListener{
		void onInsertFinish(Object sender,Model_Task[] models,Object info);
	}
	
	
	public onFinishInsertListener onFinishInsertListener;
	
	public void setOnFinishInesertListenter(onFinishInsertListener listener){
		onFinishInsertListener = listener;
	}
	
	public Task() {
		// TODO Auto-generated constructor stub
	}
	
	public Task(Context context){
		this.context = context;
		task_gs = (IDataBaseGS<Model_Task,UUID>)DataFactory.iTask_GS_factory(context);
		allocation_gs = (IDataBaseGS<Model_Allocation,UUID>)DataFactory.iAllocation_GS_factory(context);
	}
	
	@SuppressWarnings("unchecked")
	public Model_Task newTaskToDB(String title,String content,int resCount,Date deadline){
		Model_Task model = getTaskModelwithDef();
		model.title = title;
		model.description = content;
		model.deadline = deadline;
		if(null != model.title){
			model.titlefirstletter = String.valueOf(Tool.getLetterSetBelongTo(model.title.charAt(0)));
		}else {
			model.titlefirstletter = " ";
		}
		
		
		Model_Allocation aModel_Allocation = getAllocationModelwithDef();
		aModel_Allocation.task_uuid = model.uuid;
		
		task_gs.setOnInsertFinishListener(new IDataBaseGS.onInsertFinishListener() {

			@Override
			public void onInsertFinish(Object sender, int affectedRows) {
				// TODO Auto-generated method stub
				Task.this.affectedRows_task += affectedRows;
				System.out.println("111111111");
			}
			
		});
		
		allocation_gs.setOnInsertFinishListener(new IDataBaseGS.onInsertFinishListener() {

			@Override
			public void onInsertFinish(Object sender, int affectedRows) {
				// TODO Auto-generated method stub
				Task.this.affectedRows_allocation += affectedRows;
				System.out.println("22222222222");
			}
		} );
		
		if(0 != resCount){
			aModel_Allocation.count = resCount;
			model.allocations = new Model_Allocation[]{aModel_Allocation};
			allocation_gs.insertModel(aModel_Allocation);
		}
		
		
		//save
		//IDataBaseGS<Model_Task,UUID> task_gs = (IDataBaseGS<Model_Task,UUID>)DataFactory.iTask_GS_factory(context);
		task_gs.insertModel(model);
		
		
		boolean isInsert = false;
		if((0 == resCount||affectedRows_allocation  > 0 )&& affectedRows_task > 0){
			if(null != onFinishInsertListener){
				isInsert = true;
			}
		}else{ 
			if (resCount != 0 &&0 == affectedRows_allocation) {
				System.out.println("allocation have not been inserted");
			}
			if(0 == affectedRows_task){
				System.out.println("task have not been inserted");
			}
		}
		
		onFinishInsertListener.onInsertFinish(context,new Model_Task[]{model},isInsert);
		clearAffectedFlags();
		
		return model;
	}
	
	private void clearAffectedFlags(){
		affectedRows_allocation = 0;
		affectedRows_task = 0;
	}
	
	public Model_Task getTaskModelwithDef(){
		Model_Task model = new Model_Task();
		model.status = 10;
		model.recrodeDateTime = new Date();
		model.isUnexpected = false;
		model.uuid = Tool.getUuid();
		return model;
	}
	
	public Model_Allocation getAllocationModelwithDef(){
		Model_Allocation model = new Model_Allocation();
		model.uuid = Tool.getUuid();
		model.recordDatetime = new Date();
		return model;
	}
	
	public Cursor getTotalRes(UUID uuid){
		return ((ITask_GS)task_gs).getTotalRes(uuid);
	}

}
